// let indexUrl = 'https://johnjobson.github.io/ajax/number.json';
import GlobalPath from './global.path.js'
import store from '@/store'

let pid = null;
if(store.state.wxuserInfo) {
	pid = store.state.wxuserInfo.token || '';
}
let baseUrl = GlobalPath.baseUrl
const install = (Vue, vm) => {
	
	let http = (params = {method: 'get',url: '', data: {}}) => {
		console.log(params);
		if(params.url.indexOf('/sys/phoneLogin') < 0 ) {
			if(pid==""&&pid==null&&pid==undefined){
				uni.showModal({
					title: '添衣宝',
					content: '登录超时',
					showCancel: false,
					success: function () {
						// uni.reLaunch({
						// 	url: '/pages/login/login'
						// });
					}
				});
			}
		}

		return new Promise((resolve, reject) => {
			params.method = params.method.toLowerCase()
			
			console.info('aa');
			
			if (params.method === 'get' || params.method === undefined) {
				vm.$u.get(baseUrl + params.url, params.data).then(res => {
					
					if (res.statusCode == 200) {
						if(process.env.MOCK)
							resolve(res.data.body);
						else
							resolve(res);
					} else {
						// uni.reLaunch({
						// 	url: '/pages/login/login'
						// });
						resolve(res);
					}
				}).catch(res => {
					if(res.description && res.description.indexOf('timeout') !== -1){
						uni.showModal({
							title: '添衣宝',
							content: url+"请求超时，请稍后再试",
							showCancel: false
						});
					}else if(res.description && res.description.indexOf('request') !== -1){
						uni.showModal({
							title: '添衣宝',
							content: "服务器已关闭",
							showCancel: false
						});
					}else{
						// uni.showModal({
						// 	title: '添衣宝',
						// 	content: "请检查网络后重试",
						// 	showCancel: false
						// });
					}
					if(uni.getSystemInfoSync().platform == 'ios'){
						uni.vibrateShort({
							success: function () {}
						});
					}else{
						uni.vibrateLong({
							success: function () {}
						});
					}
					try{
						uni.hideLoading();
					}catch(e){
						//TODO handle the exception
					}
					reject(res)
				})
			}
			
			if (params.method === 'post') {
				console.info('cc');
				vm.$u.post(
					baseUrl + params.url,
					params.data,
					{ 'X-Requested-With': 'XMLHttpRequest','content-type': 'application/json;charset=UTF-8'},
				).then(res => {
					console.log("aaa:"+res);
					if (res.statusCode == 200) {
						if(process.env.MOCK)
							resolve(res.data.body);
						else
							resolve(res);
					} else {
						// uni.reLaunch({
						// 	url: '/pages/login/login'
						// });
						resolve(res);
					}
				}).catch(res => {
					// console.log(JSON.stringify(res))
					if(res.data.status == 500)
					{
						// uni.reLaunch({
						// 	url: '/pages/login/login'
						// });
						resolve(res);
					}
					if(res.description && res.description.indexOf('timeout') !== -1){
						uni.showModal({
							title: '添衣宝',
							content: url+"请求超时，请稍后再试",
							showCancel: false
						});
					}else if(res.description && res.description.indexOf('request') !== -1){
						uni.showModal({
							title: '添衣宝',
							content: "服务器已关闭",
							showCancel: false
						});
					}else{
						// uni.showModal({
						// 	title: '添衣宝',
						// 	content: "请检查网络后重试",
						// 	showCancel: false
						// });
					}
					if(uni.getSystemInfoSync().platform == 'ios'){
						uni.vibrateShort({
							success: function () {}
						});
					}else{
						uni.vibrateLong({
							success: function () {}
						});
					}
					try{
						uni.hideLoading();
					}catch(e){
						//TODO handle the exception
						console.log('ee')
					}
					reject(res)
				})
			}
			
			if (params.method === 'upload') { // 上传文件
				vm.$u.post(params.url, params.data, { 'Content-Type': 'multipart/form-data; boundary=something' }).then(res => {
					if (res.statusCode == 200) {
						if(process.env.MOCK)
							resolve(res.data.body);
						else
							resolve(res);
					} else {
						// uni.reLaunch({
						// 	url: '/pages/login/login'
						// });
						resolve(res);
					}
				}).catch(res => {
					if(res.description && res.description.indexOf('timeout') !== -1){
						uni.showModal({
							title: '添衣宝',
							content: url+"请求超时，请稍后再试",
							showCancel: false
						});
					}else if(res.description && res.description.indexOf('request') !== -1){
						uni.showModal({
							title: '添衣宝',
							content: "服务器已关闭",
							showCancel: false
						});
					}else{
						// uni.showModal({
						// 	title: '添衣宝',
						// 	content: "请检查网络后重试",
						// 	showCancel: false
						// });
					}
					if(uni.getSystemInfoSync().platform == 'ios'){
						uni.vibrateShort({
							success: function () {}
						});
					}else{
						uni.vibrateLong({
							success: function () {}
						});
					}
					try{
						uni.hideLoading();
					}catch(e){
						//TODO handle the exception
					}
					reject(res)
				})
			}
		})
	}
	let http1 = (params = {method: 'get',url: '', data: {}}) => {
		return new Promise((resolve, reject) => {
			params.method = params.method.toLowerCase()
			if (params.method === 'get' || params.method === undefined) {
				vm.$u.get(indexUrl + params.url, params.data).then(res => {
					resolve(res)
				}).catch(err => {
					reject(err)
				})
			}
			
			if (params.method === 'post') {
				vm.$u.get(indexUrl + params.url, params.data).then(res => {
					resolve(res)
				}).catch(err => {
					reject(err)
				})
			}
		})
	}
	
	// 将各个定义的接口名称，统一放进对象挂载到vm.$u.api(因为vm就是this，也即this.$u.api)下
	vm.$u.api = {http, http1};
}

export default {
	install
}